/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.io.IOException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author zimi
 */
public class Product {

    private int id;

    private String name;

    private Measure unitAmount;

    private int calories;

    public Product(){
        
    }
    
    public Product(KXmlParser parser) throws XmlPullParserException, IOException{
        while (parser.nextTag() != XmlPullParser.END_TAG) {
            parser.require(XmlPullParser.START_TAG, null, null);
            String tagName = parser.getName();
            String tagText = null;
            if (!tagName.equals("unitAmount")) {
                tagText = parser.nextText();
            }
            if (tagName.equals("name")) {
                this.name = tagText;
            }
            if (tagName.equals("calories")) {
                this.calories = Integer.parseInt(tagText);
            }
            if (tagName.equals("id")) {
                this.id = Integer.parseInt(tagText);
            }
            if (tagName.equals("unitAmount")) {
                this.unitAmount = new Measure(parser);
            }
            parser.require(XmlPullParser.END_TAG, null, null);
        }
    }

    public Product(String name, Measure unitAmount, int calories) {
        this.name = name;
        this.unitAmount = unitAmount;
        this.calories = calories;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCalories() {
        return calories;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Measure getUnitAmount() {
        return unitAmount;
    }

    public void setUnitAmount(Measure unitAmount) {
        this.unitAmount = unitAmount;
    }

    public boolean equals(Object obj) {
        if(!(obj instanceof Product)) {
            return false;
        }
        Product product = (Product)obj;
        return product.getName().equals(name);
    }


}
